package com.candy.control;

import com.candy.bean.Car;
import com.candy.bean.News;
import com.candy.service.CarService;
import com.candy.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("新闻信息管理")
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    NewsService newsService;
    /**
     * 接收get请求查询获取全部未删除的信息；进行分页处理
     * @param
     * @return
     */
    @GetMapping("/getallnews")
    public PageInfo<News> getAllInfoes(Integer pageNum, Integer pageSize, News news){

        PageHelper.startPage(pageNum,pageSize);

//        pageHelper.startPage下面的第一个查询会进行分页处理
        List<News> lst=newsService.getAllNews(news);
        PageInfo<News> pageInfo=new PageInfo<>(lst);
        System.out.println("pagehelper:::::::::::::::::::");
        System.out.println("total:"+pageInfo.getTotal());
        System.out.println("size:"+pageInfo.getSize());
        System.out.println("list:"+pageInfo.getList());
        System.out.println("pageInfo:"+pageInfo);
        return pageInfo;
    }


    /**
     * 新增或编辑方法；影响行数大于0；数据更新成功返回ok；反之返回eg
     * @param news
     * @return
     */
    @PostMapping("/addorupdatenews")
    public String addOrUpdateUser(@RequestBody  News news){
        Integer num=newsService.addOrUpdateNews(news);
        try {
            if(num>0){
                return "ok";
            }else {
                return "eg";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 删除方法；影响行数大于0；数据更新成功返回ok；反之返回eg
     * @param news
     * @return
     */
    @RequestMapping("/deletenews")
    public String deleteUser(@RequestBody  News news){
        Integer num=newsService.deleteNews(news);
        if(num>0){
            return "ok";
        }else {
            return "eg";
        }

    }


}
